<?
include("inc_security.php");

//Khai báo biến khi thêm mới
$gs_title				= "Cấu hình Website";
$gs_action				= getURL();
$gs_redirect			= getURL();
$gs_errorMsg			= "";

//Get data edit
$record_id				= $lang_id;
$db_edit					= new db_query("SELECT * FROM " . $gs_table . " WHERE " . $id_field . " = " . $record_id);
if(mysql_num_rows($db_edit->result) == 0){
	//Redirect if can not find data
	redirect($gs_error);
}
$edit						= mysql_fetch_array($db_edit->result);
unset($db_edit);

//Lấy dữ liệu đề giữ nguyên trạng thái khi submit error
$con_admin_email		= getValue("con_admin_email", "str", "POST", $edit["con_admin_email"]);
$con_site_title		= getValue("con_site_title", "str", "POST", $edit["con_site_title"]);
$con_meta_keywords	= getValue("con_meta_keywords", "str", "POST", $edit["con_meta_keywords"]);
$con_meta_description= getValue("con_meta_description", "str", "POST", $edit["con_meta_description"]);
$con_exchange			= getValue("con_exchange", "dbl", "POST", $edit["con_exchange"]);
$con_root_path			= getValue("con_root_path", "str", "POST", $edit["con_root_path"]);
$con_yahoo_list		= getValue("con_yahoo_list", "str", "POST", $edit["con_yahoo_list"]);
$con_skype_list		= getValue("con_skype_list", "str", "POST", $edit["con_skype_list"]);

$con_homepage_config		= getValue("con_homepage_config", "int", "POST", $edit["con_homepage_config"]);
$con_left_menu_type		= getValue("con_left_menu_type", "int", "POST", $edit["con_left_menu_type"]);
$con_introduce_type		= getValue("con_introduce_type", "int", "POST", $edit["con_introduce_type"]);
$con_intro_thumb		= getValue("con_intro_thumb", "str", "POST", $edit["con_intro_thumb"]);
$con_intro_time		= getValue("con_intro_time", "int", "POST", $edit["con_intro_time"]);
$con_raovat_homepage		= getValue("con_raovat_homepage", "int", "POST", $edit["con_raovat_homepage"]);
$con_quantity_product_page		= getValue("con_quantity_product_page", "int", "POST", $edit["con_quantity_product_page"]);
$con_message_zero_price		= getValue("con_message_zero_price", "str", "POST", $edit["con_message_zero_price"]);

$con_product_homepage_show_array = explode(',', $edit["con_product_homepage_show"]);
$con_product_homepage_show_array = getValue("con_product_homepage_show_array", "arr", "POST", $con_product_homepage_show_array);
$con_product_homepage_show = '';
for ($i=0;$i<=2;$i++)
{
	if ($con_product_homepage_show_array[$i] == 1)
	{
		$con_product_homepage_show .= '1';
	}
	else
	{
		$con_product_homepage_show .= '0';
	}
	if ($i < 2)
	{
		$con_product_homepage_show .= ',';
	}
}

$con_product_homepage_type		= getValue("con_product_homepage_type", "int", "POST", $edit["con_product_homepage_type"]);
$con_other_product		= getValue("con_other_product", "int", "POST", $edit["con_other_product"]);

$con_theme		= getValue("con_theme", "str", "POST", $edit["con_theme"]);
$con_bg_type		= getValue("con_bg_type", "int", "POST", $edit["con_bg_type"]);
$con_bg_user_fixed		= getValue("con_bg_user_fixed", "int", "POST", $edit["con_bg_user_fixed"]);
$con_bg_default_id		= getValue("con_bg_default_id", "int", "POST", $edit["con_bg_default_id"]);
$con_bg_user_upload		= getValue("con_bg_user_upload", "str", "POST", $edit["con_bg_user_upload"]);

//Cấu hình static
reset($arrStatic);
foreach($arrStatic as $key => $value){
	$$value				= getValue($value, "int", "POST", $edit[$value]);
}

//Cấu hình mail server
$con_sendmail_method	= getValue("con_sendmail_method", "int", "POST", $edit["con_sendmail_method"]);
$con_smtp_username	= getValue("con_smtp_username", "str", "POST", $edit["con_smtp_username"]);
$con_smtp_password	= getValue("con_smtp_password", "str", "POST", $edit["con_smtp_password"]);

//Get action variable for add new data
$action					= getValue("action", "str", "POST", "");
//Check $action for insert new data
if($action == "execute"){
	
	//Lấy dữ liệu kiểu checkbox
	$con_sendmail_method	= getValue("con_sendmail_method", "int", "POST", 0);
	$con_bg_user_fixed		= getValue("con_bg_user_fixed", "int", "POST", 0);
	
	/*
	Call class form:
	1). Ten truong
	2). Ten form
	3). Kieu du lieu , 0 : string , 1 : kieu int, 2 : kieu email, 3 : kieu double, 4 : kieu hash password
	4). Noi luu giu data  0 : post, 1 : variable
	5). Gia tri mac dinh, neu require thi phai lon hon hoac bang default
	6). Du lieu nay co can thiet hay khong
	7). Loi dua ra man hinh
	8). Chi co duy nhat trong database
	9). Loi dua ra man hinh neu co duplicate
	*/
	$myform = new generate_form();
	//Add table insert data
	$myform->addTable($gs_table);
	$myform->add("con_admin_email", "con_admin_email", 2, 1, "", 1, "Địa chỉ email của Admin không hợp lệ.", 0, "");
	$myform->add("con_site_title", "con_site_title", 0, 1, "", 0, "", 0, "");
	$myform->add("con_meta_keywords", "con_meta_keywords", 0, 1, "", 0, "", 0, "");
	$myform->add("con_meta_description", "con_meta_description", 0, 1, "", 0, "", 0, "");
	$myform->add("con_exchange", "con_exchange", 3, 1, "", 0, "", 0, "");
	$myform->add("con_root_path", "con_root_path", 0, 1, "", 0, "", 0, "");
	$myform->add("con_yahoo_list", "con_yahoo_list", 0, 1, "", 0, "", 0, "");
	$myform->add("con_skype_list", "con_skype_list", 0, 1, "", 0, "", 0, "");
	$myform->add("con_sendmail_method", "con_sendmail_method", 1, 1, 0, 0, "", 0, "");
	$myform->add("con_smtp_username", "con_smtp_username", 0, 1, "", 0, "", 0, "");
	$myform->add("con_smtp_password", "con_smtp_password", 0, 1, "", 0, "", 0, "");
	
	$myform->add("con_homepage_config", "con_homepage_config", 1, 1, 0, 0, "", 0, "");
	$myform->add("con_left_menu_type", "con_left_menu_type", 1, 1, 0, 0, "", 0, "");
	$myform->add("con_introduce_type", "con_introduce_type", 1, 1, 0, 0, "", 0, "");
	$myform->add("con_intro_thumb", "con_intro_thumb", 0, 1, "", 0, "", 0, "");
	$myform->add("con_intro_time", "con_intro_time", 1, 1, 0, 0, "", 0, "");
	$myform->add("con_raovat_homepage", "con_raovat_homepage", 1, 1, 0, 0, "", 0, "");
	$myform->add("con_quantity_product_page", "con_quantity_product_page", 1, 1, 0, 0, "", 0, "");
	$myform->add("con_message_zero_price", "con_message_zero_price", 0, 1, "", 0, "", 0, "");
	$myform->add("con_product_homepage_show", "con_product_homepage_show", 0, 1, "", 0, "", 0, "");
	$myform->add("con_product_homepage_type", "con_product_homepage_type", 1, 1, 0, 0, "", 0, "");
	$myform->add("con_other_product", "con_other_product", 1, 1, 0, 0, "", 0, "");
	
	$myform->add("con_theme", "con_theme", 0, 1, "", 0, "", 0, "");
	$myform->add("con_bg_type", "con_bg_type", 1, 1, 0, 0, "", 0, "");
	$myform->add("con_bg_user_fixed", "con_bg_user_fixed", 1, 1, 0, 0, "", 0, "");
	$myform->add("con_bg_default_id", "con_bg_default_id", 1, 1, 0, 0, "", 0, "");
	
	//Cấu hình static
	reset($arrStatic);
	foreach($arrStatic as $key => $value){
		$myform->add($value, $value, 1, 1, 0, 0, "", 0, "");
	}

	//Check form data
	$gs_errorMsg .= $myform->checkdata();
	
	//Get $filename
    $filename = '';
    if ($gs_errorMsg == '')
    {
        $upload = new upload($gs_fieldupload, $gs_filepath, $gs_extension, $gs_filesize, $gs_insert_logo);
        $filename = $upload->file_name;
        $gs_errorMsg .= $upload->warning_error;
    }
	
	if($gs_errorMsg == ""){
		
		if($filename != ""){
			//Upload new image
			$$gs_fieldupload = $filename;
			$myform->add($gs_fieldupload, $gs_fieldupload, 0, 1, "", 0, "", 0, "");
		}//End if($filename != "")
		
		//Insert to database
		$myform->removeHTML(0);
		$db_update = new db_execute($myform->generate_update_SQL($id_field, $record_id));
		unset($db_update);
		
		//Redirect after insert complate
		redirect($gs_redirect);
		
	}//End if($gs_errorMsg == "")
	unset($myform);
	
}//End if($action == "insert")
?>
<html>
<head>
<title><?=$gs_title?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css" media="all">@import "../css/GSportal.css";</style>
<script language="javascript" src="../js/library.js"></script>
</head>
<body>
<div class="bg_title_content">
<div class="content_title" style="float:left"><?=$gs_title?></div>
</div>
<div align="center" class="content">
<?
$counter = 0;
$db_visited	= new db_query("SELECT vis_counter FROM visited");
if($visited	= mysql_fetch_array($db_visited->result)){
	$counter = number_format($visited["vis_counter"],0,".",",");
}
unset($db_visited);
$form = new form();
$form->create_form("edit", $gs_action, "post", "multipart/form-data");
$form->create_table();
?>
<?=$form->create_control("Số lượt truy cập", '<font style="color:#FF0000; font-weight:bold">' . $counter . '</font>')?>
<?=$form->text_note('Những ô có dấu sao (<font class="form_asterisk">*</font>) là bắt buộc phải nhập.')?>
<?=$form->errorMsg($gs_errorMsg)?>
<tr><td class="table_title_1" colspan="2">- Cấu hình chung -</td></tr>
<?=$form->text("Admin email", "con_admin_email", "con_admin_email", $con_admin_email, "Admin email", 1, 200, "", 255, "", "", "")?>
<?=$form->text("Tiêu đề Website", "con_site_title", "con_site_title", $con_site_title, "Tiêu đề Website", 0, 350, "", 255, "", "", "")?>
<?=$form->textarea("Meta Keyword", "con_meta_keywords", "con_meta_keywords", $con_meta_keywords, "Meta Keyword", 0, 350, 75, "", "", "")?>
<?=$form->textarea("Meta Description", "con_meta_description", "con_meta_description", $con_meta_description, "Meta Description", 0, 350, 100, "", "", "")?>
<?=$form->text("Tỉ giá", "con_exchange", "con_exchange", $con_exchange, "Tỉ giá", 0, 70, "", 20, "", "", "")?>
<?=$form->text("Đường dẫn gốc", "con_root_path", "con_root_path", $con_root_path, "Đường dẫn gốc", 0, 200, "", 255, "", "", "")?>
<tr><td class="table_title_1" colspan="2">- Hỗ trợ online -</td></tr>
<?=$form->textarea("Danh sách Yahoo", "con_yahoo_list", "con_yahoo_list", $con_yahoo_list, "Danh sách Yahoo", 0, 350, 75, "", "", "")?>
<?=$form->textarea("Danh sách Skype", "con_skype_list", "con_skype_list", $con_skype_list, "Danh sách Skype", 0, 350, 75, "", "", "")?>
<tr><td class="table_title_1" colspan="2">- Cấu hình trang chủ -</td></tr>
<?=$form->select("Cấu hình hiển thị trang chủ", "con_homepage_config", "con_homepage_config", $arrHomepageConfig, $con_homepage_config, "Cấu hình hiển thị trang chủ", 0, "", 1, 0, '', '')?>
<?=$form->select("Kiểu menu bên trái", "con_left_menu_type", "con_left_menu_type", $arrLeftMenuType, $con_left_menu_type, "Kiểu menu bên trái", 0, "", 1, 0, '', '')?>
<?=$form->select("Giới thiệu trang chủ", "con_introduce_type", "con_introduce_type", $arrIntroType, $con_introduce_type, "Giới thiệu trang chủ", 0, "", 1, 0, 'onChange="change_introduce_type(this.value)"', '')?>
<tr>
	<td id="introduce_text" class="form_name">Cấu hình giới thiệu trang chủ :</td>
	<td class="form_text">
		<script type="text/javascript">
		function change_introduce_type(type){
			ob = document.getElementById("introduce_config");
			ob.style.display = (type == 3) ? "block" : "none";
		}
		</script>
		<table id="introduce_config" cellpadding="2" cellspacing="2" style="display:none">
			<tr>
				<td class="form_text">Vị trí danh sách ảnh thumb :</td>
				<td class="form_text">
					<select class="form_control" name="con_intro_thumb">
                    <? foreach ($arrIntroThumb as $key => $value) {?>
                    <option value="<?=$key?>"<?=($key==$con_intro_thumb)?' selected="selected"':''?>><?=$value?></option>
					<? }?>
                    </select>
				</td>
			</tr>
			<tr>
				<td class="form_text">Thời gian tự động chuyển ảnh :</td>
				<td class="form_text">
					<select class="form_control" name="con_intro_time">
                    <? foreach ($arrIntroTime as $key => $value) {?>
                    <option value="<?=$key?>"<?=($key==$con_intro_time)?' selected="selected"':''?>><?=$value?></option>
					<? }?>
                    </select> giây
				</td>

			</tr>
		</table>
	</td>
</tr>
<? //$form->select("Số tin rao vặt ở trang chủ", "con_raovat_homepage", "con_raovat_homepage", $arrRaovatHomepage, $con_raovat_homepage, "Số tin rao vặt ở trang chủ", 0, "", 1, 0, '', ' tin')?>
<tr><td class="table_title_1" colspan="2">- Cấu hình sản phẩm -</td></tr>
<?=$form->select("Số sản phẩm trong 1 trang", "con_quantity_product_page", "con_quantity_product_page", $arrQuantityProductPage, $con_quantity_product_page, "Số sản phẩm trong 1 trang", 0, "", 1, 0, '', ' sản phẩm')?>
<?=$form->text("Thông báo khi giá SP bằng 0", "con_message_zero_price", "con_message_zero_price", $con_message_zero_price, "Thông báo khi giá SP bằng 0", 0, 250, "", 255, "", "", " <i>(Ví dụ: Vui lòng gọi)</i>")?>
<?=$form->checkbox("Những tab sản phẩm hiển thị trang chủ", "con_product_homepage_show_0" . $form->ec . "con_product_homepage_show_1" . $form->ec . "con_product_homepage_show_2", "con_product_homepage_show_array[0]" . $form->ec . "con_product_homepage_show_array[1]" . $form->ec . "con_product_homepage_show_array[2]", 1 . $form->ec . 1 . $form->ec . 1, implode($form->ec, explode(',', $con_product_homepage_show)), "Sản phẩm xem nhiều nhất" . $form->ec . "Sản phẩm mới" . $form->ec . "Sản phẩm khuyến mại", 0, "" . $form->ec . "" . $form->ec, "")?>
<?=$form->select("Sản phẩm trang chủ", "con_product_homepage_type", "con_product_homepage_type", $arrProductHomepageType, $con_product_homepage_type, "Sản phẩm trang chủ", 0, "", 1, 0, '', '')?>
<?=$form->select("Những sản phẩm khác", "con_other_product", "con_other_product", $arrOtherProduct, $con_other_product, "Những sản phẩm khác", 0, "", 1, 0, '', '')?>
<tr><td class="table_title_1" colspan="2">- Hình nền -</td></tr>
<?
$onChange = "change_bg_type(this.value)";
if ($con_bg_user_fixed == 1)
{
	$checked = ' checked="checked"';
}
else
{
	$checked = '';
}
?>
<?=$form->select("Kiểu hình nền", "con_bg_type", "con_bg_type", $arrBgType, $con_bg_type, "Kiểu hình nền", 0, "", 1, 0, 'onChange="' . $onChange . '"', '<input type="checkbox" id="con_bg_user_fixed" name="con_bg_user_fixed" value="1"' . $checked . ' /><label for="con_bg_user_fixed">Hình nền cố định (không thay đổi khi kéo trang xuống dưới)</label>')?>
<tr>
	<td class="form_name">Chọn hình nền mặc định :</td>
    <td>
    	<style type="text/css">
		.background_default_review{
			background:#FFFFFF;
			border:2px #CCCCCC solid;
			float:left;
			margin-left:10px;
			width:400px;
			height:332px;
			max-height:326px;
		}
		</style>
        <div style="float:left">
        <?
        $db_bg = new db_query('SELECT * FROM user_background WHERE ub_default = 1 AND ub_active = 1 ORDER BY ub_order ASC');
		?>
        	<select class="form_control" id="con_bg_default_id" name="con_bg_default_id"<?=($con_bg_type != 1)?' disabled="disabled"':''?> size="25" onChange="change_background_default(this.value)">
            <? while ($row = mysql_fetch_array($db_bg->result)) {?>
            <option value="<?=$row['ub_id']?>"<?=($row['ub_id']==$con_bg_default_id)?' selected="selected"':''?>><?=$row['ub_name']?></option>
			<? }?>
            </select>
        <?
		unset($db_bg);
		?>
        </div>
        <div id="background_default_review" class="background_default_review"></div>
        <script language="javascript">
		var bgDefault			= <?=$con_bg_default_id?>;
		var bgUserUploadSrc	= "../../user_background/<?=$con_bg_user_upload?>";
		var arrBGDefault		= Array();
		<?
		$db_bg = new db_query('SELECT * FROM user_background WHERE ub_default = 1 AND ub_active = 1 ORDER BY ub_order ASC');
		while ($row = mysql_fetch_array($db_bg->result))
		{
		?>
		arrBGDefault[<?=$row['ub_id']?>]	= "<?=$row['ub_picture']?>";
		<?
		}
		unset($db_bg);
		?>
		function change_bg_type(id){
			ob1 = document.getElementById("con_bg_type");
			ob2 = document.getElementById("con_bg_default_id");
			ob3 = document.getElementById("con_bg_user_upload");
			ob4 = document.getElementById("background_default_review");
			
			ob2.disabled = true;
			ob3.disabled = true;
			if(id == 0){
				ob4.style.background = "#FFFFFF";
			}
			else if(id == 1){
				ob2.disabled = false;
				change_background_default(bgDefault);
			}
			else if(id == 2){
				ob4.style.background = "#FFFFFF";
				change_bg_user_upload(bgUserUploadSrc);
				ob3.disabled = false;
			}
		}
		
		function change_background_default(id){
			ob = document.getElementById("background_default_review");
			if(typeof(arrBGDefault[id]) !== undefined){
				ob.style.background = "url(../../user_background/" + arrBGDefault[id] + ") repeat";
				bgDefault	= id;
			}
		}
		
		function change_bg_user_upload(src){
			ob = document.getElementById("background_default_review");
			if(src != ""){
				ob.style.background = "url(" + src + ") repeat";
				bgUserUpload = src;
			}
		}
		<? if ($con_bg_type == 1) {?>
		change_background_default(bgDefault)
		<? } elseif ($con_bg_type == 2) {?>
		change_bg_user_upload(bgUserUploadSrc)
		<? }?>
		</script>
    </td>
</tr>
<?=$form->getFile("Upload hình nền", "con_bg_user_upload", "con_bg_user_upload", "Upload hình nền", 0, 32, "", '<br />(Dung lượng tối đa <font color="#FF0000">' . $gs_filesize . ' Kb</font>)')?>
<tr><td class="table_title_1" colspan="2">- Cấu hình trang tĩnh -</td></tr>
<?
$db_static	= new db_query("SELECT cat_id, cat_name, sta_id, sta_title
									 FROM categories_multi, statics_multi
									 WHERE cat_id = sta_category_id AND cat_type = 'static'
									 ORDER BY cat_order ASC, cat_id ASC, sta_order ASC");
reset($arrStatic);
foreach($arrStatic as $key => $value){
	if(mysql_num_rows($db_static->result) > 0) mysql_data_seek($db_static->result, 0);
?>
<tr>
	<td class="form_name"><?=$key?> :</td>
	<td class="form_text">
		<select title="Chọn trang tĩnh" id="<?=$value?>" name="<?=$value?>" class="form_control">
			<option value="0">--[Chọn trang tĩnh]--</option>
			<?
			$cat_id = 0;
			while($row = mysql_fetch_array($db_static->result)){
				if($cat_id != $row["cat_id"]){
					$cat_id = $row["cat_id"];
					echo '<optgroup label="' . $row["cat_name"] . '"></optgroup>';
				}
				$selected = ($$value == $row["sta_id"]) ? ' selected="selected"' : '';
				echo '<option title="' . htmlspecialbo($row["sta_title"]) . '" value="' . $row["sta_id"] . '"' . $selected . '>&nbsp; &nbsp;|-- ' . cut_string($row["sta_title"], 55) . '</option>';
			}
			?>
		</select>
	</td>
</tr>
<?
}
unset($db_static);
?>
<tr><td class="table_title_1" colspan="2">- Cấu hình Mail Server -</td></tr>
<tr>
	<td class="form_name">SMTP Mail Server :</td>
	<td class="form_text">
		<input type="checkbox" id="con_sendmail_method" name="con_sendmail_method" value="1"<? if($con_sendmail_method != 0){echo ' checked="checked"';}?> onclick="check_method()" /> &nbsp;
		Username: <input type="text" id="con_smtp_username" name="con_smtp_username" value="<?=$con_smtp_username?>"<? if($con_sendmail_method == 0){echo ' disabled="disabled"';}?> class="form_control" style="width:100px" /> &nbsp;
		Password: <input type="password" value="<?=$con_smtp_password?>"<? if($con_sendmail_method == 0){echo ' disabled="disabled"';}?> id="con_smtp_password" name="con_smtp_password" class="form_control" style="width:100px" />
	</td>
</tr>
<script language="javascript">
function check_method(){
	if(document.getElementById("con_sendmail_method").checked == true){
		document.getElementById("con_smtp_username").disabled = false;
		document.getElementById("con_smtp_password").disabled = false;
		document.getElementById("con_smtp_username").focus();
	}
	else{
		document.getElementById("con_smtp_username").disabled = true;
		document.getElementById("con_smtp_password").disabled = true;
	}
}
</script>
<?=$form->button("submit" . $form->ec . "reset", "submit" . $form->ec . "reset", "submit" . $form->ec . "reset", "Cập nhật" . $form->ec . "Làm lại", "Cập nhật" . $form->ec . "Làm lại", 'style="background:url(' . $gs_imagepath . 'button_1.gif) no-repeat"' . $form->ec . 'style="background:url(' . $gs_imagepath . 'button_2.gif)"', "");?>
<?=$form->hidden("action", "action", "execute", "");?>
<?
$form->close_table();
$form->close_form();
unset($form);
?>
</div>
</body>
</html>
<script language="javascript">ButtonLeftFrame();</script>